/**
 *<p>Copyright: CHINA NATIONAL SOFTWARE & SERVICE CO.,LTD.</p>
 */
package com.cssnb.commons.shiro;

import java.security.MessageDigest;
import java.util.Arrays;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @Company: 中国软件与技术服务股份有限公司宁波子公司
 * @Project: 宁波市国税-外维系统
 * @Module ID:
 * @Description:
 * @JDK version used: JDK1.4
 * @Author: 卜繁晟
 * @Email: 5545795@qq.com | bufsh@css.com.cn
 * @Created Date: 2013-4-25 上午10:13:47
 */
public class PasswordCheckUtil {

	protected static final String ALGORITHM = "MD5";

	public static boolean checkPassword(String password, String dbpassword, String oldsalt) throws Exception{
		BASE64Decoder dncoder = new BASE64Decoder();
		byte[] bsalt = dncoder.decodeBuffer(oldsalt);
		MessageDigest md = MessageDigest.getInstance(ALGORITHM);
		md.update(bsalt);
		md.update(password.getBytes("UTF8"));
		byte[] digest = md.digest();
		BASE64Encoder encoder = new BASE64Encoder();
		String digestS = encoder.encode(digest);
		byte[] dbdigest = dbpassword.getBytes();
		byte[] olddigest = digestS.getBytes();
		if(Arrays.equals(olddigest, dbdigest)){
			return true;
		}else{
			return false;
		}
	}
}
